package com.qdsgjsfk.vision.ui;

import android.widget.EditText;
import android.widget.TextView;

import com.qdsgjsfk.vision.R;
import com.rest.business.RestProxy;
import com.rest.response.BaseResponse;
import com.rest.response.UserInfoResponse;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import jz.joyoung.robot.util.LogUtil;
import com.qdsgjsfk.vision.base.BaseActivity;
import com.qdsgjsfk.vision.util.NetUtil;

/**
 * Created by HP on 2019/1/4.
 */

public class UserInfoActivity extends BaseActivity {
    @BindView(R.id.edt_name)
    EditText edt_name;
    @BindView(R.id.edt_email)
    EditText edt_email;
    @BindView(R.id.edt_phone)
    EditText edt_phone;
    @BindView(R.id.man)
    TextView man;
    @BindView(R.id.woman)
    TextView woman;

    String sex = "男";

    @Override
    protected void initViews() {

    }

    @Override
    protected void initData() {
        getUserInfo();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_user_info;
    }


    private void getUserInfo(){
        RestProxy.getInstance().getUserInfo( new Observer<UserInfoResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(UserInfoResponse userInfoResponse) {
                edt_name.setText(userInfoResponse.data.realName);
                edt_email.setText(userInfoResponse.data.email);
                edt_phone.setText(userInfoResponse.data.phone);
                if(userInfoResponse.data.sex.equals("男")){
                    man.setBackgroundResource(R.drawable.item_bg_blue_button);
                    woman.setBackgroundResource(R.drawable.item_bg_grey_button);
                    man.setTextColor(getResources().getColor(R.color.white));
                    woman.setTextColor(getResources().getColor(R.color.commonBlack));
                }else{
                    woman.setBackgroundResource(R.drawable.item_bg_blue_button);
                    man.setBackgroundResource(R.drawable.item_bg_grey_button);
                    woman.setTextColor(getResources().getColor(R.color.white));
                    man.setTextColor(getResources().getColor(R.color.commonBlack));
                }
            }

            @Override
            public void onError(Throwable e) {
                NetUtil.onError(e, UserInfoActivity.this);
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @OnClick(R.id.man)
    void man() {
        man.setBackgroundResource(R.drawable.item_bg_blue_button);
        woman.setBackgroundResource(R.drawable.item_bg_grey_button);
        man.setTextColor(getResources().getColor(R.color.white));
        woman.setTextColor(getResources().getColor(R.color.commonBlack));
        sex = "男";
    }

    @OnClick(R.id.woman)
    void woman() {
        woman.setBackgroundResource(R.drawable.item_bg_blue_button);
        man.setBackgroundResource(R.drawable.item_bg_grey_button);
        woman.setTextColor(getResources().getColor(R.color.white));
        man.setTextColor(getResources().getColor(R.color.commonBlack));
        sex = "女";
    }

    @OnClick(R.id.btn_confirm)
    void btn_confirm(){
        updateUserInfo();
    }

    private void updateUserInfo(){
        RestProxy.getInstance().updateUserInfo(edt_name.getText().toString().trim(), edt_phone.getText().toString().trim(), sex, edt_email.getText().toString().trim(), new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                    finish();
            }

            @Override
            public void onError(Throwable e) {
                NetUtil.onError(e, UserInfoActivity.this);
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LogUtil.e( "UserInfoActivity-------","onDestroy" );
    }
}
